package cn.tedu.controller;

import cn.tedu.controller.BaseAndEx.BaseController;
import cn.tedu.entity.Address;
import cn.tedu.entity.Cart;
import cn.tedu.service.port.AddressService;
import cn.tedu.service.port.CartService;
import cn.tedu.service.port.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Resource
    private OrderService orderService;
    @Resource
    private AddressService addressService;
    @Resource
    private CartService cartService;

    @RequestMapping("/confirm.do")
    public String handleConfirmOrder(
            HttpSession session, String cartId, ModelMap modelMap) {

        //******* 获取用户的收获人地址列表 *******
        Integer uid = getUidSession(session);
        //根据uid获取收获人地址列表
        List<Address> addresses = addressService.selectAddress(uid);

        //******* 获取所选的购物车商品信息 *******
        //拆分cartid 前端提交的是多个
        String[] ids = cartId.split(",");
        Cart cart;
        //信息可能是多个，声明集合存储
        List<Cart> carts = new ArrayList<Cart>();
        for (String id:ids) {
            cart = cartService.getCartById(uid, Integer.valueOf(id));
            carts.add(cart);
        }

        modelMap.addAttribute("carts", carts);
        modelMap.addAttribute("addresses", addresses);
        return "";
    }

    public static void main(String[] args) {
        Random ran = new Random();
        int a = ran.nextInt(10);
    }
}
